package edu.umd.mhealth.lab6;


import org.scribe.builder.ServiceBuilder;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

import edu.umd.mhealth.oauth.FitbitApi;
import edu.umd.mhealth.oauth.OAuthAccessTokenActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment extends Fragment {
	private ViewGroup rootView;
	
	private static String apiKey = "24c744f75f98440bb6f528fc1e1c3bdb";  // application's key 
	private static String apiSecret = "a1f69039a1364a39b604df2fdf1a1ae8"; // application's secret 
	private static String callback = "activity_main:///"; // site after verification
	private OAuthService service = new ServiceBuilder()  // OAuth settings for fitbit
	.provider(FitbitApi.class)
	.apiKey(apiKey) 
	.apiSecret(apiSecret) 
	.callback(callback) 
	.debug() 
	.build();

		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		rootView = (ViewGroup) inflater.inflate(R.layout.widget, container);
		
		final Button login = (Button) rootView.findViewById(R.id.login);
		login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ((Token) ((MainActivity) getActivity()).accessToken == null){ 
					login(v); 
				} else {
					Toast.makeText(rootView.getContext(), "You are already logged in.", 30).show();
				}				 
			}
		});


		final Button getButton = (Button) rootView.findViewById(R.id.get);
		getButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Token accessToken = (Token) ((MainActivity) getActivity()).accessToken;
				if (accessToken != null){ 
					new HttpGetTask(rootView, service, accessToken).execute("http://api.fitbit.com/1/user/-/body/log/weight/date/2013-10-21.json");
					
				} else {
					Toast.makeText(rootView.getContext(), "please login", 30).show();
				}
			}
		});
		
		final Button historyButton = (Button) rootView.findViewById(R.id.history);
		historyButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Token accessToken = (Token) ((MainActivity) getActivity()).accessToken;
				if (accessToken != null){ 
					new HttpGetHistoryTask(rootView, service, accessToken).execute("http://api.fitbit.com/1/user/-/body/weight/date/2013-10-25/7d.json");
					
				} else {
					Toast.makeText(rootView.getContext(), "please login", 30).show();
				}
			}
		});


		final Button postButton = (Button) rootView.findViewById(R.id.post);
		postButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Token accessToken = (Token) ((MainActivity) getActivity()).accessToken;
				if (accessToken != null){ 
					new HttpPostTask(rootView, service, accessToken).execute("http://api.fitbit.com/1/user/-/body/log/weight.json?weight=180&date=2013-11-04");
				}else {
					Toast.makeText(rootView.getContext(), "please login", 30).show();
				}
			}
		});
		
		
		
		Token accessToken = (Token) ((MainActivity) getActivity()).accessToken;
		/*if (accessToken == null) {
			Log.d("AccessToken if statement!!", "hey");
			LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.display);
			TextView tv = new TextView(rootView.getContext());
			tv.setText("Please Login");
			layout.addView(tv);
		}
		
		Log.d("AccessToken", accessToken.toString());
		View view = (View) rootView.findViewById(R.id.display);	
		if ((savedInstanceState != null) && (view instanceof TextView)){
			String text = savedInstanceState.getString("text");		
			((TextView) view).setText(text);
		} else if (savedInstanceState != null) {
			
		}*/
		
		Log.d("oncreateView", "");
		return rootView;
	}
	
	@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        
    }


	/* Begins the login process by opening the OAuthAccessTokenActivity for user authentication */
	public void login(View view){
		Intent i = new Intent(rootView.getContext(), OAuthAccessTokenActivity.class);
		i.putExtra("apiKey", apiKey);
		i.putExtra("apiSecret", apiSecret);
		i.putExtra("callback", callback);
		startActivityForResult(i, 0);
	}

}



